package com.xiaohua.tlgorders.service.impl;


import com.xiaohua.tlgorders.entity.Address;
import com.xiaohua.tlgorders.mapper.AddressMapper;
import com.xiaohua.tlgorders.service.AddressService;
import com.xiaohua.tlgorders.tools.Record;
import com.xiaohua.tlgorders.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Record selAddress(Integer userId) {
        Record record=new Record();
        List<Address> list=null;
        try {
            list=addressMapper.selAddress(userId);
        }catch (Exception e){
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("用户查询地址异常！");
            e.printStackTrace();
            return record;
        }

        if (null != list && list.size()>0){
            record.setMsg(Status.SELECT_SUCCESS);
            record.setMsg("收货地址查询成功1");
            record.setData(list);;
        }else{
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("收货地址查询失败！");
        }
        return record;
    }

    @Override
    public Record addAddress(Address address) {
        Record record=new Record();
        int count=0;
        try {
            count=addressMapper.addAddress(address);
        }catch (Exception e){
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("用户添加地址异常!");
            e.printStackTrace();
        }

        if (count>0){
            record.setStatus(Status.SAVE_SUCCESS);
            record.setMsg("用户添加地址成功!");
            record.setData(count);;
        }else{
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("用户添加地址失败!");
        }
        return record;
    }

    @Override
    public Record modifyAddress(Address address) {
        Record record=new Record();
        int count=0;
        try {
            count=addressMapper.modifyAddress(address);
        }catch (Exception e){
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("用户修改地址异常!");
            e.printStackTrace();
        }

        if (count>0){
            record.setMsg(Status.SAVE_SUCCESS);
            record.setStatus("用户修改地址成功！");
            record.setData(count);;
        }else{
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("用户修改地址失败!");
        }
        return record;
    }

    @Override
    public Record deleteAddress(Integer addressId) {
        Record record=new Record();
        int count=0;
        try {
            count=addressMapper.deleteAddress(addressId);
        }catch (Exception e){
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("用户删除地址异常!");
            e.printStackTrace();
        }

        if (count>0){
            record.setMsg(Status.SAVE_SUCCESS);
            record.setStatus("用户删除地址成功！");
            record.setData(count);;
        }else{
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("用户删除地址失败!");
        }
        return record;
    }
}
